﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.Common;
using Attendences.Framework.Entity;
using System.Data;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

public partial class Page_AttendencePlanExport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        txtDeptDisplay.Attributes.Add("readonly", "readonly");
        txtDisplay.Attributes.Add("readonly", "readonly");
        txtStartDate.Attributes.Add("readonly", "readonly");
        txtEndDate.Attributes.Add("readonly", "readonly");
    }
    private void LoadData()
    {
        string WhereClause = "1=1";
        if (txtStartDate.Text != string.Empty)
        {
            WhereClause += string.Format(" and AttendDate >='{0}'", txtStartDate.Text);
        }
        if (txtEndDate.Text != string.Empty)
        {
            WhereClause += string.Format(" and AttendDate <='{0}'", txtEndDate.Text);
        }
        if (txtName.Value != string.Empty)
        {
            WhereClause += string.Format(" and UserName in ('{0}')", string.Join("','",txtName.Value.Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries)));
        }
        if (txtDeptDisplay.Text != string.Empty)
        {
            WhereClause += string.Format(" and DepartmentID in('{0}')", string.Join("','", txtDeptID.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)));
        }
        DataTable dt = DataAccess.Select(typeof(View_Attend_All), WhereClause,"UserDisplayName");
        DataTable dtCount = DataAccess.ExecuteSql("Attendences.ConnectionString", string.Format("select count(UserDisplayName) as DataCount,UserDisplayName from View_Attend_All where {0} group by UserDisplayName", WhereClause)).Tables[0];

        ViewState["dtData"] = dtCount;
        if (dt != null) lbCount.Text = dtCount.Rows.Count.ToString();
        rptInfo.DataSource = dtCount;
        rptInfo.DataBind();
        CreateChart(dtCount);
    }
    private void CreateChart(DataTable data)
    {
        Series s = new Series();
        s.ChartType = SeriesChartType.Column;
        s.XValueType = ChartValueType.String;
        s.CustomProperties = "DrawingStyle=Emboss";
        foreach (DataRow dr in data.Rows)
        {
            DataPoint dp = new DataPoint();
            dp.Color = GetRandomColor(Convert.ToInt32(dr["DataCount"]));
            dp.YValues = new double[] { Convert.ToDouble(dr["DataCount"]) };
            dp.LegendText = dr["UserDisplayName"].ToString();
            dp.AxisLabel = dr["UserDisplayName"].ToString();
            s.Points.Add(dp);
        }
        chart.Series.Add(s);
    }
    public Color GetRandomColor(int seed)
    {
        Random RandomNum_First = new Random(seed);
        Random RandomNum_Sencond = new Random(seed*seed);

        int int_Red = RandomNum_First.Next(256);
        int int_Green = RandomNum_Sencond.Next(256);
        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
        int_Blue = (int_Blue > 255) ? 255 : int_Blue;

        return Color.FromArgb(int_Red, int_Green, int_Blue);
    }
    protected void lbtnExport_Click(object sender, EventArgs e)
    {
        LoadData();
        if (ViewState["dtData"] != null)
        {
            string filename = "值班人员统计";
            if (txtDisplay.Text != string.Empty)
            {
                filename += txtDisplay.Text + "的" + filename;
            }
            DataTable dtData = (DataTable)ViewState["dtData"];
            StringWriter sw = new StringWriter();
            if (dtData != null && dtData.Rows.Count > 0)
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename) + ".xls");
                Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                sw.WriteLine(filename);
                sw.WriteLine("值班人员\t值班次数");
                foreach (DataRow dr in dtData.Rows)
                {
                    sw.WriteLine(dr["UserDisplayName"].ToString() + "\t" + dr["DataCount"].ToString());
                }
                sw.WriteLine("小计:\t" + dtData.Rows.Count + "人");
                sw.Close();
                Response.Write(sw);
                Response.End();
            }
        }
    }
    protected void lbtnSearch_Click(object sender, EventArgs e)
    {
        LoadData();
    }
}
